package com.xindaoapp.happypet.shop.ui;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.adapter.XinDaoBaseAdapter;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.entry.CommentInfos;
import com.xindaoapp.happypet.entry.CommentInfos.Info.CommentInfo;
import com.xindaoapp.happypet.utils.IRequest;

/**
 * 商品详情评论列表
 * 
 * @author xindaoapp
 * 
 */
public class ProductDetailCommentAdapter extends XinDaoBaseAdapter<CommentInfo> {
    private final String goodsID;

    public ProductDetailCommentAdapter(String goodsID, Context context, List<CommentInfo> iniData,
            int pageSize, int res, int loadingRes) {
        super(context, iniData, pageSize, res, loadingRes);
        this.goodsID = goodsID;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent, CommentInfo value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();

        if (holder == null) {
            holder = new ViewHolder();
            holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.content = (TextView) convertView.findViewById(R.id.content);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.standard = (TextView) convertView.findViewById(R.id.standard);
            holder.starLevel = (RatingBar) convertView.findViewById(R.id.star_level);
            convertView.setTag(holder);
        }

        ImageLoader.getInstance().displayImage(value.user_logo, holder.icon);
        holder.name.setText(value.user_name);
        holder.time.setText(value.add_time);
        holder.content.setText(value.content);
        // holder.standard.setText("尺码");
        holder.starLevel.setRating(Float.parseFloat(value.comment_rank));

        return convertView;
    }

    class ViewHolder {
        ImageView icon;
        TextView name;
        TextView content;
        TextView time;
        TextView standard;
        RatingBar starLevel;
    }

    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<CommentInfo> iLoadNextPageData) {
        super.nextPage(start, size, iLoadNextPageData);
        HappyPetApplication.get().getMoccaApi()
                .getShopDetailComment(goodsID, 0, start, size, new IRequest<CommentInfos>() {

                    @Override
                    public void request(CommentInfos t) {
                        iLoadNextPageData.loadNextPageData(t == null ? null : t.data.info);
                    }
                });
    }

}
